package com.aba.dormitory.system.admin;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

// 账户类，用于存储不同类型的账户信息
class Account {
    private String username;
    private String password;
    private String accountType;

    public Account(String username, String password, String accountType) {
        this.username = username;
        this.password = password;
        this.accountType = accountType;
    }

    // Getters and Setters
    public String getUsername() { return username; }
    public String getPassword() { return password; }
    public String getAccountType() { return accountType; }
}

// 账户管理类，用于管理所有账户
class AccountManager {
    private static List<Account> accounts = new ArrayList<>();

    public static void addAccount(Account account) {
        accounts.add(account);
        System.out.println("添加新账户: " + account.getUsername() + " (" + account.getAccountType() + ")");
    }

    public static boolean isUsernameExists(String username) {
        return accounts.stream().anyMatch(a -> a.getUsername().equals(username));
    }

    public static List<Account> getAccountsByType(String accountType) {
        return accounts.stream()
                .filter(a -> a.getAccountType().equals(accountType))
                .toList();
    }
}

public class AdminFrame extends JFrame {

    public AdminFrame() {
        showAccountSelectionPanel(); // 直接显示账号选择页面
    }

    private void showAccountSelectionPanel() {
        JFrame selectionFrame = new JFrame("选择管理账号类型");
        selectionFrame.setSize(350, 350);
        selectionFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        selectionFrame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(6, 1, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50));

        JLabel titleLabel = new JLabel("请选择要管理的账号类型", JLabel.CENTER);
        titleLabel.setFont(new Font("宋体", Font.BOLD, 16));
        
        JButton teacherButton = new JButton("教师账号管理");
        JButton studentButton = new JButton("学生账号管理");
        JButton staffButton = new JButton("职工账号管理");
        JButton addAccountButton = new JButton("添加新账号");
        JButton exitButton = new JButton("退出程序"); // 替换返回登录按钮为退出按钮

        Font buttonFont = new Font("宋体", Font.PLAIN, 14);
        teacherButton.setFont(buttonFont);
        studentButton.setFont(buttonFont);
        staffButton.setFont(buttonFont);
        addAccountButton.setFont(buttonFont);
        exitButton.setFont(buttonFont);

        panel.add(titleLabel);
        panel.add(teacherButton);
        panel.add(studentButton);
        panel.add(staffButton);
        panel.add(addAccountButton);
        panel.add(exitButton);

        selectionFrame.add(panel);
        selectionFrame.setVisible(true);

        teacherButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                selectionFrame.dispose();
                showAdminPanel("教师账号管理");
            }
        });

        studentButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                selectionFrame.dispose();
                showAdminPanel("学生账号管理");
            }
        });

        staffButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                selectionFrame.dispose();
                showAdminPanel("职工账号管理");
            }
        });

        addAccountButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                selectionFrame.dispose();
                showAddAccountPanel();
            }
        });

        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    private void showAddAccountPanel() {
        JFrame addAccountFrame = new JFrame("添加新账号");
        addAccountFrame.setSize(400, 300);
        addAccountFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        addAccountFrame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(6, 2, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        JLabel typeLabel = new JLabel("账号类型:");
        String[] accountTypes = {"教师", "学生", "职工"};
        JComboBox<String> typeComboBox = new JComboBox<>(accountTypes);
        
        JLabel usernameLabel = new JLabel("用户名:");
        JTextField usernameField = new JTextField();
        
        JLabel passwordLabel = new JLabel("密码:");
        JPasswordField passwordField = new JPasswordField();
        
        JLabel confirmLabel = new JLabel("确认密码:");
        JPasswordField confirmField = new JPasswordField();
        
        JButton addButton = new JButton("添加");
        JButton cancelButton = new JButton("取消");

        panel.add(typeLabel);
        panel.add(typeComboBox);
        panel.add(usernameLabel);
        panel.add(usernameField);
        panel.add(passwordLabel);
        panel.add(passwordField);
        panel.add(confirmLabel);
        panel.add(confirmField);
        panel.add(new JLabel());
        panel.add(addButton);
        panel.add(new JLabel());
        panel.add(cancelButton);

        addAccountFrame.add(panel);
        addAccountFrame.setVisible(true);

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String accountType = (String) typeComboBox.getSelectedItem();
                String username = usernameField.getText().trim();
                String password = new String(passwordField.getPassword());
                String confirmPassword = new String(confirmField.getPassword());

                if (username.isEmpty()) {
                    JOptionPane.showMessageDialog(addAccountFrame, 
                            "用户名不能为空", "验证错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (password.isEmpty()) {
                    JOptionPane.showMessageDialog(addAccountFrame, 
                            "密码不能为空", "验证错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (!password.equals(confirmPassword)) {
                    JOptionPane.showMessageDialog(addAccountFrame, 
                            "两次输入的密码不一致", "验证错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (AccountManager.isUsernameExists(username)) {
                    JOptionPane.showMessageDialog(addAccountFrame, 
                            "该用户名已存在", "验证错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                Account newAccount = new Account(username, password, accountType);
                AccountManager.addAccount(newAccount);

                JOptionPane.showMessageDialog(addAccountFrame, 
                        "账号 " + username + " (" + accountType + ") 已成功添加", 
                        "添加成功", JOptionPane.INFORMATION_MESSAGE);

                addAccountFrame.dispose();
                showAccountSelectionPanel();
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addAccountFrame.dispose();
                showAccountSelectionPanel();
            }
        });
    }

    private void showAdminPanel(String accountType) {
        JFrame adminPanel = new JFrame(accountType);
        adminPanel.setSize(400, 350);
        adminPanel.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        adminPanel.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(5, 1, 0, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50));

        JButton initializeButton = new JButton("初始化" + accountType);
        JButton lockUnlockButton = new JButton("冻结/解冻" + accountType);
        JButton viewLogButton = new JButton("查看" + accountType + "活跃日志");
        JButton viewAccountsButton = new JButton("查看所有" + accountType);
        JButton backButton = new JButton("返回");

        Font buttonFont = new Font("宋体", Font.PLAIN, 14);
        initializeButton.setFont(buttonFont);
        lockUnlockButton.setFont(buttonFont);
        viewLogButton.setFont(buttonFont);
        viewAccountsButton.setFont(buttonFont);
        backButton.setFont(buttonFont);

        panel.add(initializeButton);
        panel.add(lockUnlockButton);
        panel.add(viewLogButton);
        panel.add(viewAccountsButton);
        panel.add(backButton);

        adminPanel.add(panel);
        adminPanel.setVisible(true);

        initializeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int confirm = JOptionPane.showConfirmDialog(adminPanel, 
                        "确定要初始化所有" + accountType + "吗？此操作不可撤销。", 
                        "确认初始化", JOptionPane.YES_NO_OPTION);
                
                if (confirm == JOptionPane.YES_OPTION) {
                    JOptionPane.showMessageDialog(null, "已初始化" + accountType, "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

        lockUnlockButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "已对" + accountType + "进行冻结/解冻操作", "提示", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        viewLogButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "已显示" + accountType + "活跃日志", "提示", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        viewAccountsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Account> accounts = AccountManager.getAccountsByType(
                    accountType.contains("教师") ? "教师" : 
                    accountType.contains("学生") ? "学生" : "职工"
                );

                if (accounts.isEmpty()) {
                    JOptionPane.showMessageDialog(adminPanel, 
                            "暂无" + accountType + "记录", "查看结果", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }

                StringBuilder message = new StringBuilder(accountType + "列表：\n\n");
                for (Account account : accounts) {
                    message.append("用户名：").append(account.getUsername()).append("\n");
                    message.append("密码：").append("********").append("\n");
                    message.append("-----------------\n");
                }

                JTextArea textArea = new JTextArea(message.toString());
                textArea.setEditable(false);
                JScrollPane scrollPane = new JScrollPane(textArea);
                scrollPane.setPreferredSize(new Dimension(300, 200));

                JOptionPane.showMessageDialog(adminPanel, scrollPane, 
                        accountType + "列表", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                adminPanel.dispose();
                showAccountSelectionPanel();
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                AdminFrame frame = new AdminFrame();
            }
        });
    }
}
 


